<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./php/ajax.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
    商品名称：<input type="text" class="pname"><br>
    商品介绍：<input type="text" class="pname1"><br>
    价格：<input type="text" class="price"><br>
    图片1：<input class="imgFile" type="file" name="file" multiple  /><br>
    <div>
        <button class="btn" type="submit">提交</button>
    </div>
    <script>
        let btnEle = document.querySelector(".btn")
        let pnameEle = document.querySelector(".pname")
        let pname1Ele = document.querySelector(".pname1")
        let priceEle = document.querySelector(".price")
        let imgFileEle = document.querySelector(".imgFile");
        btnEle.onclick=function(){
            let pname = pnameEle.value;
            let pname1 = pname1Ele.value;
            let price = priceEle.value;
            let imgFile = imgFileEle.files[0];
            let imgFile2 = imgFileEle.files[1];
            console.log(imgFile)
            // let imgFile2 = imgFileEle.files[1]
            let form = new FormData();
				// 把数据和文件加入到form
				form.append("img",imgFile);
				form.append("img2",imgFile2);
                form.append("pname",pname);
                form.append("price",price);
                form.append("pname1",pname1);
				let xhr = new XMLHttpRequest();
				xhr.open("POST","php/add.php");
                xhr.send(form);
				xhr.onload = function(){
					// console.log(xhr.responseText);
				}
        }



    </script>
</body>
</html>